package com.tianda.his.modules.system.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.tianda.his.commons.constant.Constants;
import com.tianda.his.commons.vo.AjaxResult;
import com.tianda.his.modules.system.dto.LoginDto;
import com.tianda.his.modules.system.entity.ActiveUser;
import com.tianda.his.modules.system.service.ISysUserService;
import com.tianda.his.modules.system.vo.MenuTreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping
public class LoginController {

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 登录
     * @param loginDto
     * @return
     */
    @PostMapping("/login/doLogin")
    public AjaxResult doLogin(@RequestBody LoginDto loginDto) {
        sysUserService.login(loginDto);
        //获取登录成功的令牌
        String token = StpUtil.getTokenValue();
        AjaxResult success = AjaxResult.success();
        success.put(Constants.TOKEN,token);

        return success;
    }

    @PostMapping("/login/logout")
    public AjaxResult logout() {
        StpUtil.logout();
        return AjaxResult.success();
    }

    /**
     * 获取登录用户信息
     * @return
     */
    @GetMapping("/login/getInfo")
    public AjaxResult getInfo() {
        ActiveUser activeUser = sysUserService.getInfo();

        AjaxResult success = AjaxResult.success();
        success.put("username",activeUser.getUsername());
        success.put("picture",activeUser.getPicture());
        success.put("roles",activeUser.getRoles());
        success.put("permissions",activeUser.getPermissions());

        return success;
    }
    /**
     * 获取菜单
     * @return
     */
    @GetMapping("/login/getMenus")
    public AjaxResult getMenus() {

        List<MenuTreeVo> menuTreeVos = sysUserService.getMenus();

        AjaxResult success = AjaxResult.success(menuTreeVos);

        return success;
    }

}
